package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author with葡萄成熟时
 * @version 1.0
 */

@RestController("userOrderController")
@Slf4j
@Api(tags = "用户端订单相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @ApiOperation("用户下单")
    @PostMapping("/user/order/submit")
    public Result<OrderSubmitVO> orderSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        OrderSubmitVO orderSubmitVO = orderService.orderSubmit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/user/order/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        //TODO 跳过验证
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }


    /**
     * @description: 历史订单查询
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午2:50 2024/11/18
     */
    @GetMapping("/user/order/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> getOrderHistory(int page, int pageSize, Integer status) {


        OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
        ordersPageQueryDTO.setPage(page);
        ordersPageQueryDTO.setPageSize(pageSize);
        ordersPageQueryDTO.setStatus(status);
        PageResult result = orderService.getOrderHistory(ordersPageQueryDTO);

        return Result.success(result);
    }
/**
 * @description: 查询订单详情
 * @param:
 * @return:
 * @author Wait葡萄成熟时
 * @date: 下午3:18 2024/11/18
 */
    @GetMapping("/user/order/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> selectOrder(@PathVariable String id){


        OrderVO orderVO=  orderService.selectOrder(id);


        return Result.success(orderVO);
    }

    /**
     * @description: 取消订单
     * @param:
     * @return:
     * @author Wait葡萄成熟时
     * @date: 下午3:36 2024/11/18
     */

    @PutMapping("/user/order/cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancelOrder(@PathVariable Long id){

        orderService.cancelOrder(id);

        return Result.success();

    }
    
    /** 
     * @description: 再来一单
     * @param:
     * @return: 
     * @author Wait葡萄成熟时
     * @date: 下午4:01 2024/11/18
     */
    @PostMapping("/user/order/repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetitionOrder(@PathVariable Long id){

        orderService.repetitionOrder(id);

        return Result.success();

    }
/**
 * @description 客户催单
 * @author Wait葡萄成熟时
 * @date 下午9:06 2024/11/24
 */
    @GetMapping("/user/order/reminder/{id}")
    @ApiOperation("催单")
    public Result reminderOrder(@PathVariable Long id){

        orderService.reminderOrder(id);
        return Result.success();
    }

}
